package com.liu.communitysystemend.module.repair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.communitysystemend.exception.CommonException;
import com.liu.communitysystemend.exception.SavaException;
import com.liu.communitysystemend.module.repair.domain.Repair;
import com.liu.communitysystemend.module.repair.domain.RepairDto;
import com.liu.communitysystemend.module.repair.mapper.RepairMapper;
import com.liu.communitysystemend.module.repair.service.RepairService;
import com.liu.communitysystemend.module.user.domain.UserDto;
import com.liu.communitysystemend.utils.AuthUtils;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
*
*/
@Service
public class RepairServiceImpl implements RepairService {

    @Autowired
    private RepairMapper repairMapper;

    @Autowired
    private AuthUtils authUtils;
    
    @Override
    public Repair getRepairById(String id) {
        if (id == null) {
            throw new CommonException(501, "参数为空");
        }
        LambdaQueryWrapper<Repair> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Repair::getId,id);
        Repair repair = repairMapper.selectOne(wrapper);
        return repair;
    }

    @Override
    public Repair update(Repair repair) {
        LambdaQueryWrapper<Repair> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Repair::getId,repair.getId());
        int update = repairMapper.update(repair, wrapper);
        if (update>0){
            return repair;
        }
        throw new CommonException(502,"更新失败");
    }

    @Override
    public Repair add(Repair repair) {
        repair.setStatus(0);
        repair.setApplyTime(new Date());
        repair.setUserId(authUtils.getUid());
        if(repair ==null){
            throw new SavaException("插入用户失败,菜单的类型实体为空");
        }
        int insert=repairMapper.insert(repair);
        if(insert>0){
            return repair;
        }
        throw new SavaException("插入用户失败");
    }

    @Override
    public Page<Repair> getPage(Repair repair) {
        LambdaQueryWrapper<Repair> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Repair::getIsDeleted,0);
        if (repair.getUserId()!=null || repair.getUserId()!=""){
            wrapper.eq(Repair::getUserId,repair.getUserId());
        }
        wrapper.orderByDesc(Repair::getApplyTime);
        Page<Repair> page=new Page<>(repair.getPage().getPage(),repair.getPage().getSize());
        Page<Repair> poiPage=(Page<Repair>) repairMapper.selectPage(page,wrapper);
        return poiPage;
    }

    @Override
    public Integer deleteById(String id) {
        Repair repair = selectOneById(id);
        if(repair==null){
            throw new CommonException(4004,"找不到id为:"+id+"的数据");
        }
        int i=repairMapper.deleteById(id);
        return i;
    }

    @Override
    public Integer test() {
        for (int i = 0; i < 20; i++) {
            Repair repair = new Repair();
            repair.setId("100"+i);
            repair.setApplyTime(new Date());
            repair.setStatus(0);
            repair.setUserId("100"+i);
            repair.setReason("水龙头坏了"+i);
            repair.setLocation("1栋2楼101房");
            repairMapper.insert(repair);
        }
        return 1;
    }

    @Override
    public Page<RepairDto> getAllPage(Repair repair) {
        Page<RepairDto> userDtoPage = new Page<>(repair.getPage().getPage(),repair.getPage().getSize());
        return repairMapper.getAllPage(userDtoPage,repair);

    }

    public Repair selectOneById(String id){
        Repair repair=repairMapper.selectById(id);
        return  repair;
    }
}
